﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Back.Xinkui.Common;
using CommonFramework.Utility;
using System.Transactions;
using Back.Xinkui.Entity.Buylead;
using DataFrameworkAccess;
namespace Back.Xinkui.DataAccess.Buylead
{
    public class BuyleadDataAccess
    {
        public static List<BuyleadListEntity> GetBuyleadList(string where, Pager page)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Buylead.Query.GetBuyleadList");
            cmd.SetParameterValue("@PageIndex", page.PageIndex);
            cmd.SetParameterValue("@WHERE", where);
            cmd.SetParameterValue("@PageSize", page.PageSize);
            var list = cmd.ExecuteEntityList<BuyleadListEntity>();

            page.TotalCount = cmd.GetParameterValue("@AllCount").TryType<int>(0);
            return list;
        }
        /// <summary>
        /// 获取询盘信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static BuyleadListEntity GetBuyleadById(int ID)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Buylead.Query.GetBuyleadById");
            cmd.SetParameterValue("@BuyleadID", ID);

            return cmd.ExecuteEntity<BuyleadListEntity>();
        }

        /// <summary>
        /// 新增询盘信息
        /// </summary>
        /// <param name="entity"></param>
        public static void CreateBuyleadInfo(BuyleadListEntity entity)
        {
            using (TransactionScope tran = new TransactionScope())
            {
                try
                {
                    DataCommand cmd = DataCommandManager.GetDataCommand("Buylead.Action.CreateBuyleadInfo");

                    cmd.SetParameterValue("@Subject", entity.Subject);
                    cmd.SetParameterValue("@CategoryID", entity.CategoryID);
                    cmd.SetParameterValue("@MainKeyword", entity.MainKeyword);
                    cmd.SetParameterValue("@Keywords", entity.Keywords);
                    cmd.SetParameterValue("@PurchaseType", entity.PurchaseType);
                    cmd.SetParameterValue("@Details", entity.Details);
                    cmd.SetParameterValue("@OEMNO", entity.OEMNO);
                    cmd.SetParameterValue("@Packing", entity.Packing);
                    cmd.SetParameterValue("@Quantity", entity.Quantity);
                    cmd.SetParameterValue("@ValidTime", entity.ValidTime);
                    cmd.SetParameterValue("@PriceRange", entity.PriceRange);
                    cmd.SetParameterValue("@CertificationRequirements", entity.CertificationRequirements);
                    cmd.SetParameterValue("@OrderQuantity", entity.OrderQuantity);
                    cmd.SetParameterValue("@AdminID", entity.AdminID);
                    cmd.SetParameterValue("@Country", entity.Country);
                    cmd.ExecuteNonQuery();

                    tran.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    tran.Dispose();
                }
            }
        }
        /// <summary>
        /// 保存询盘信息
        /// </summary>
        /// <param name="entity"></param>
        public static void SaveBuyleadInfo(BuyleadListEntity entity)
        {
            using (TransactionScope tran = new TransactionScope())
            {
                try
                {
                    DataCommand cmd = DataCommandManager.GetDataCommand("Buylead.Action.SaveBuyleadInfo");

                    cmd.SetParameterValue("@BuyleadID", entity.BuyleadID);
                    cmd.SetParameterValue("@Subject", entity.Subject);
                    cmd.SetParameterValue("@CategoryID", entity.CategoryID);
                    cmd.SetParameterValue("@MainKeyword", entity.MainKeyword);
                    cmd.SetParameterValue("@Keywords", entity.Keywords);
                    cmd.SetParameterValue("@PurchaseType", entity.PurchaseType);
                    cmd.SetParameterValue("@Details", entity.Details);
                    cmd.SetParameterValue("@OEMNO", entity.OEMNO);
                    cmd.SetParameterValue("@Packing", entity.Packing);
                    cmd.SetParameterValue("@Quantity", entity.Quantity);
                    cmd.SetParameterValue("@PriceRange", entity.PriceRange);
                    cmd.SetParameterValue("@CertificationRequirements", entity.CertificationRequirements);
                    cmd.SetParameterValue("@OrderQuantity", entity.OrderQuantity);
                    cmd.SetParameterValue("@AdminID", entity.AdminID);
                    cmd.SetParameterValue("@Country", entity.Country);
                    cmd.SetParameterValue("@UserID", entity.UserID);
                    cmd.SetParameterValue("@TempUserId", entity.TempUserId);
                    cmd.SetParameterValue("@FirstName", entity.FirstName);
                    cmd.SetParameterValue("@LastName", entity.LastName);
                    cmd.SetParameterValue("@Email", entity.Email);
                    cmd.SetParameterValue("@PicImgs", entity.PicImgs);
                    cmd.ExecuteNonQuery();

                    tran.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    tran.Dispose();
                }
            }

        }

        /// <summary>
        /// 更改询盘状态——询盘审核
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static void UpdateBuyleadStatus(int ID, int status, int userId)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Buylead.Action.UpdateBuyleadStatus");
            cmd.SetParameterValue("@BuyleadID", ID);
            cmd.SetParameterValue("@AuditStatus", status);
            cmd.SetParameterValue("@AdminID", userId);

            cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// 删除询盘信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static void DeleteBuylead(int companyID, int userId)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Buylead.Action.DeleteBuylead");
            cmd.SetParameterValue("@BuyleadID", companyID);
            cmd.SetParameterValue("@AdminID", userId);

            cmd.ExecuteNonQuery();
        }
    }
}
